
#include "Item.h"
#include "game.h"

Item::Item()
{
	
}


void Item::Init(int type, float posX, float posY)
{
	m_type = type;
	m_posX = posX;
	m_posY = posY;
	m_radius = 0.28f;

	if(m_type >= ITEM_BALL1 && m_type <ITEM_COUNT)
	{
		sprintf(m_itemName,"Balloon%d",m_type-ITEM_BALL1);
		m_toughRegion = Region(m_itemName,SHAPE_CIRCLE,m_posX,m_posY,-1,-1,m_radius);
		getTouchInput().AddRegion(m_toughRegion);
	}

	if(getType() <= ITEM_BALL3)
		m_powerMax = 2;
	else
		m_powerMax = 1;
	m_power = m_powerMax;
	m_powerCharge = 0;

}

void Item::SetReady(bool b)
{
	if(m_bReady == b)
		return;
	m_bReady = b;
	if(m_bReady)
		getTouchInput().RemoveRegion(m_itemName);
	else 
		getTouchInput().AddRegion(m_toughRegion);
}
int Item::DownPower()
{
	m_power--;

	if(m_power == 0)
	{
		getTouchInput().RemoveRegion(m_itemName);
	}

	return m_power;
}
void Item::Update()
{
	if(getTouchInput().IsRegionDown(m_itemName))
	{
		if(m_type >= ITEM_BALL1 && m_type <ITEM_COUNT)
		{
			GetItemManager()->SetCrtBalloon(m_type - ITEM_BALL1);
		}
	}

	if(m_power<m_powerMax)
	{
		m_powerCharge += getGame()->GetGameplayTimeStep();

		if(m_powerCharge > 5.0f)	//5s
		{
			if(m_power == 0)
			{
				getTouchInput().AddRegion(m_toughRegion);
				if(getType() == ITEM_BALL1)
				{
					GetItemManager()->GetItem(4)->SetReady(true);
					GetItemManager()->GetItem(5)->SetReady(true);
				}
				if(getType() == ITEM_BALL2)
				{
					GetItemManager()->GetItem(3)->SetReady(true);
					GetItemManager()->GetItem(5)->SetReady(true);
				}
				if(getType() == ITEM_BALL3)
				{
					GetItemManager()->GetItem(3)->SetReady(true);
					GetItemManager()->GetItem(4)->SetReady(true);
				}
			}
			m_power++;
			m_powerCharge = 0;
		}
	}
}

void Item::Draw()
{

	float ap2 = m_radius + 0.02f;
	GetDebugDraw().DrawRect(b2Vec2(m_posX - ap2,m_posY - ap2),b2Vec2(m_posX + ap2,m_posY + ap2),b2Color(0.0f,1.0f,0.0f));
	

	GetDebugDraw().FillRect(b2Vec2(m_posX - ap2 - 0.04f,m_posY - ap2),b2Vec2(m_posX - ap2,m_posY - ap2 + (ap2*2 * m_powerCharge/5.0f)),b2Color(0.0f,1.0f,0.0f));
	
	int i = 0;
	while(i<m_power)
	{
		GetDebugDraw().FillRect(b2Vec2(m_posX + i*0.07f- ap2,m_posY-ap2 -0.1f),b2Vec2(m_posX + i*0.07f + 0.05f- ap2,m_posY-ap2 + 0.05f-0.1f),b2Color(1.0f,1.0f,0.0f));
		i++;
	}

	while(i<m_powerMax)
	{
		GetDebugDraw().FillRect(b2Vec2(m_posX + i*0.07f- ap2,m_posY-ap2 -0.1f),b2Vec2(m_posX + i*0.07f + 0.05f- ap2,m_posY-ap2 + 0.05f-0.1f),b2Color(0.4f,0.4f,0.4f));
		i++;
	}

	b2Color color;
	if(m_type == ITEM_BALL1)
		color = b2Color(1.0f,0.0f,0.0f);
	else if(m_type == ITEM_BALL2)
		color = b2Color(0.0f,1.0f,0.0f);
	else if(m_type == ITEM_BALL3)
		color = b2Color(0.0f,0.0f,1.0f);
	else if(m_type == ITEM_BALL4)
		color = b2Color(0.0f,1.0f,1.0f);
	else if(m_type == ITEM_BALL5)
		color = b2Color(1.0f,0.0f,1.0f);
	else if(m_type == ITEM_BALL6)
		color = b2Color(1.0f,1.0f,0.0f);

	if(GetItemManager()->GetCurrentBall() &&
		GetItemManager()->GetCurrentBall()->m_refItem == this)
		GetDebugDraw().DrawCircle(b2Vec2(m_posX,m_posY),m_radius+0.05f,color);
	else
		GetDebugDraw().DrawCircle(b2Vec2(m_posX,m_posY),m_radius,color);
	

	/*if(m_power == 0 || !m_bReady)
	{
		GetDebugDraw().DrawRect(b2Vec2(m_posX+0.02f,m_posY-m_radius),b2Vec2(m_posX + 0.02f + m_radius*2,m_posY + m_radius),b2Color(0.4f,0.4f,0.4f));		
	}*/
}